package org.atlas.framework.ds.impl;

import org.atlas.framework.ds.DataSourceFactory;
import org.atlas.framework.helper.ConfigHelper;

import javax.sql.DataSource;

/**
 * 数据源抽象工厂
 *
 * //TODO 多数据源支持
 *
 * Created by suxinjie on 2017/3/5.
 */
public abstract class AbstractDataSourceFactory<T extends DataSource> implements DataSourceFactory{

    protected final String driver = ConfigHelper.getJdbcDriver();
    protected final String url = ConfigHelper.getJdbcUrl();
    protected final String username = ConfigHelper.getJdbcUsername();
    protected final String password = ConfigHelper.getJdbcPassword();

    @Override
    public DataSource getDataSource() {
        T ds = createDataSource();
        setDriver(ds, driver);
        setUrl(ds, url);
        setUsername(ds, username);
        setPassword(ds, password);
        setAdvancedConfig(ds);

        return ds;
    }

    // FIXME 结合下面方法,对外提供一个或者2个即可.

    public abstract T createDataSource();

    public abstract void setDriver(T ds, String driver);

    public abstract void setUrl(T ds, String url);

    public abstract void setUsername(T ds, String username);

    public abstract void setPassword(T ds, String password);

    public abstract void setAdvancedConfig(T ds);
}
